package com.th.supcom.pds.phamapply.client.widget.phamapplydetail;

import java.util.Date;

import net.carefx.fc.framework.web.gwt.util.FcUtils;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.th.supcom.pds.common.client.commom.PdsClientDateUtils;

/**
 * 药品请领事件监听
 * 
 * @author 杨冲
 */
public class PhamApplyListener
{
    private PhamApplyButtons m_phamApplyButtons;
    private PhamApplyAction m_phamApplyAction;
    private PhamApplyFields m_phamApplyFields;

    /**
     * 设置药品请领按钮组件
     * 
     * @param phamapplyButtons
     */
    public void setphamapplyButtons (PhamApplyButtons phamapplyButtons)
    {

        this.m_phamApplyButtons = phamapplyButtons;
    }

    /**
     * 设置药品请领事件
     * 
     * @param phamapplyAction
     */
    public void setphamapplyAction (PhamApplyAction phamapplyAction)
    {

        this.m_phamApplyAction = phamapplyAction;
    }

    /**
     * 设置药品请领文本组件
     * 
     * @param phamapplyFields
     */
    public void setphamapplyFields (PhamApplyFields phamapplyFields)
    {

        this.m_phamApplyFields = phamapplyFields;
    }

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        addDispenseDrugDaysListener ();
        addQueryInpPhamApplyDetail ();
        addSaveInpPhamApplyDetail ();
        addClearAll ();
    }

    /**
     * 设置摆药天数事件监听，当摆药天数加1时，截止时间增加1天。
     */
    public void addDispenseDrugDaysListener ()
    {
        m_phamApplyFields.getDispenseDays ().addListener (Events.OnClick, new Listener <BaseEvent> ()
        {

            @Override
            public void handleEvent (BaseEvent be)
            {
                Integer dayAdd = (Integer) m_phamApplyFields.getDispenseDays ().getValue ();
                Date newDate = PdsClientDateUtils.getDateWithDayAdd (FcUtils.getCurrentServerDateTimeForClient (), dayAdd);
                m_phamApplyFields.getDisepenseEndTime ().setValue (newDate);
            }
        });
    }

    /**
     * 添加查询住院请领明细事件
     */
    public void addQueryInpPhamApplyDetail ()
    {
        m_phamApplyButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_phamApplyAction.findPhamApplyDetailInfo ();
            }
        });
    }

    /**
     * 添加保存住院请领信息事件监听
     */
    public void addSaveInpPhamApplyDetail ()
    {
        m_phamApplyButtons.getReceiveButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_phamApplyAction.savePhamApplyDetail ();
            }
        });
    }

    /**
     * 添加清屏按钮事件监听
     */
    public void addClearAll ()
    {
        m_phamApplyButtons.getDetailClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_phamApplyAction.clear ();
            }
        });
    }
}
